#!/bin/python
#-*- coding:utf-8 -*-
# Filename:    main.py
# Revision:    1.0
# Date:        2012-06-14
# Author:      simonzhang
# web:         www.simonzhang.net
# Email:       simon-zzm@163.com
### END INIT INFO
import sys
import os
import tornado.ioloop
import tornado.locale
import tornado.web
from dbmodel import *
from interface import *

def now_time():
    from time import strftime
    return strftime('%Y-%m-%d %H:%M:%S')

class GetDeteleFileStatusHandler(BaseHandler):
    @tornado.web.authenticated
    def get(self):
        _get_user = self.check_user()
        _get_status_log = self.get_argument('r')
        file=open("tmplog/%s.log"%_get_status_log, "rb").readlines()
        _delete_file_log_context=""
        for i in file:
            _delete_file_log_context+=i
        self.write(_delete_file_log_context)
     


class DelFileHandler(BaseHandler):
    @tornado.web.authenticated
    def get(self):
        _get_user = self.check_user()
        _domain, _title = if_get_domain_title()
        _get_user_left_menu = if_get_user_left_menu(_get_user)
        _get_user_manage_project = if_get_user_project(_get_user)
        self.render("templates/del_file.html", domain=_domain, title=_title, \
                                              username = _get_user, \
                                              items=_get_user_left_menu, \
                                              pros=_get_user_manage_project)
    def post(self):
        _get_user = self.check_user()
        _domain, _title = if_get_domain_title()
        _get_user_left_menu = if_get_user_left_menu(_get_user)
        _get_user_manage_project = if_get_user_project(_get_user)
        _get_project_name = self.get_arguments('s')[0]
        if u"%s" % _get_project_name == u"选择项目":
            self.write(u"别瞎操作")
            return
        try:
            _get_reboot_checkbox = self.get_argument('checkbox')
        except:
            _get_reboot_checkbox="off"
        if _get_reboot_checkbox != "on":
            _get_reboot_checkbox = 'off'
        _del_file_list = self.get_arguments("textarea")[0]
        _rm_file_list = ""
        for lo in _del_file_list.split("\r\n"):
             if len(lo) != 0:
                _rm_file_list += "%s " % lo
        from time import strftime
        _package_time = strftime('%Y%m%d%H%M%S')
        _get_local_project = "%s_%s" % (_get_project_name, _package_time)
        if not os.path.isdir('project/%s' % _get_project_name):
            os.mkdir('project/%s' % _get_project_name)
        else:
            try:
                os.system('/bin/cp -rf project/%s project/%s' % (_get_project_name, \
                                                                 _get_local_project))
            except:
                pass
            try:
                os.system('/bin/cp -rf project/%s project/%s_bak' % (_get_project_name, \
                                                                     _get_local_project))
            except:
                pass
        root_path = os.getcwd()
        file_list = ''
        for lo in _del_file_list.split("\r\n"):
            filter_base = ['rm', 'cp', 'mv', '-r', '-rf']
            if len([all for all in lo.split(' ') if all in filter_base]) > 0:
                lo = ''
            file_list += lo +" "
            try:
                os.system("rm -rf %s/project/%s/%s" % (root_path ,\
                                                _get_local_project, lo))
            except:                             
                pass
            try:
                os.system("rm -rf %s/project/%s/%s" % (root_path ,\
                                                _get_project_name, lo))
            except:
                pass
        ver_f = open("project/%s/ver.txt" % _get_local_project, "wb")
        ver_f.write("%s\n" % _package_time)
        ver_f.write("%s\n" % _rm_file_list)
        ver_f.close()
        os.system("/bin/cp project/%s/ver.txt project/%s/" % (_get_local_project, _get_project_name))
        idb_insert_new_package_name(_get_project_name, _package_time, \
                                    "1", 9, "del:%s" %file_list)
        _project_id = if_get_project_id(_get_project_name)
        _get_service_list = odb_project_service_list(_project_id)
        _get_project_info = odb_project_info(_get_project_name)
        _get_wait_time = '%s' % _get_project_info['wait_time']
        _get_thread_num = '%s' % _get_project_info['thread_num']
        if _get_thread_num == "0":
            _type = "s%s" % _get_wait_time
        else:
            _type = "t%s" % _get_thread_num
        log_file = open("tmplog/%s.log" % _get_local_project, "ab")
        log_file.write('%s Use %s on %s project del file %s\n' % \
                                                              (now_time(), _get_user, \
                                                               _get_project_name, \
                                                               _rm_file_list))
        log_file.close() 
        os.system("python %s/bin/backruncomm.py -p %s -d \"%s\" -r %s -c \"\" -t %s -l %s -v %s &" % \
                    (root_path, _project_id, _rm_file_list, _get_reboot_checkbox, \
                              _type, _get_local_project, _get_local_project))
        self.render("templates/runstatus.html", domain=_domain, title=_title, \
                                                username = _get_user, \
                                                items=_get_user_left_menu, \
                                                log_file=_get_local_project, \
                                                loginterface='get_delete_file_status')
